from time import sleep
import scrapy
import os
from eolcrawl.spiderutils.common import build_url



class AgirosRealsePkgSpider(scrapy.Spider):
    name = "agiros_realse_pkg"
    allowed_domains = ["1.94.193.239"]
    start_urls = ["http://1.94.193.239/yumrepo/agiros/openeuler2403lts/x86_64/dailybuild/"]
    # brach = os.environ["ROS_BRANCH"]
    type = 'query_spider'
    branch = os.getenv("ROS_BRANCH","humble")
    ros_prefix = os.getenv("ROS_REPOS_NAME","ros")
    
    def parse(self, response):
        
        sleep(2)
        # 使用 CSS 选择器
        rpm_links = response.css('a[href$=".rpm"]:contains(".rpm")::attr(href)').getall()
        rpm_texts = response.css('a[href$=".rpm"]::text').getall()

        # 或者使用 XPath 选择器
        # rpm_links = response.xpath('//a[contains(@href, ".rpm")]/@href').getall()
        # rpm_texts = response.xpath('//a[contains(@href, ".rpm")]/text()').getall()
        #agiros-loong-aandd-ekew-driver-py-0.0.2-1.x86_64.rpm
        self.logger.info(f"rpm_links len:{len(rpm_links)},rpm_texts len:{len(rpm_texts)}")
        for link, text in zip(rpm_links, rpm_texts):
            link = link.strip()
            text = text.strip()
            pkg_name = get_pkg_info(text)
            
            ros_full_name = self.ros_prefix+"-"+self.branch+"-"+pkg_name.ros_name
            url = build_url(response.url, link)
            self.logger.debug(f"ros pkg info:{pkg_name},ros_full_name:{ros_full_name},full_url:{url}")
            yield {
                'url': url,
                'name': ros_full_name,
                'org_pkg_name':text,
                'branch':pkg_name.branch,
                'version':pkg_name.version,
                "cpu":pkg_name.cpu_arch,
                "release_num":pkg_name.release_num
            }
            
class AgirosPkgName():
    #前缀
    prifix:str=""
    #分支
    branch:str =""
    #ros pkg name
    ros_name:str =""
    #version
    version:str =""
    #release num
    release_num = ""
    #cpu 架构
    cpu_arch = ""
    #全名
    full_name =""


def get_pkg_info(ros_name):
    # ros_name ="agiros-loong-aandd-ekew-driver-py-0.0.2-1.x86_64.rpm"
    #"agiros-loong-aandd-ekew-driver-py-0.0.2-1.x86_64.rpm"
    agi_ros_pkg = AgirosPkgName()
    
    split_name = ros_name.split("-",maxsplit=2)
    if len(split_name) > 2:
        agi_ros_pkg.prifix = split_name[0]
        agi_ros_pkg.branch = split_name[1]
        tmp_name = split_name[2]
        
        split_name2 = ros_name.rsplit("-",maxsplit=2)
        agi_ros_pkg.full_name = split_name2[0]
        #cpu 架构
        tmp_cpu_arch = tmp_name.rsplit(".",maxsplit=2)
        if len(tmp_cpu_arch) > 1:
            agi_ros_pkg.cpu_arch = tmp_cpu_arch[1]
        #版本号
        tmp_version = tmp_cpu_arch[0].rsplit("-",maxsplit=2)
        if len(tmp_version)>2:
            agi_ros_pkg.version = tmp_version[1]
            agi_ros_pkg.release_num = tmp_version[2]
            agi_ros_pkg.ros_name = tmp_version[0]
        # print(name2)
        
    else:
        print("error")
        return None
    
    return agi_ros_pkg

# if __name__ == "__main__":
#     ros_name ="agiros-loong-aandd-ekew-driver-py-0.0.2-1.x86_64.rpm"
#     get_pkg_info(ros_name)
    
    
    
    
#     pass